


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: RMIDataStore</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.remote</a> ]
</div>

<h1>Coverage Summary for Class: RMIDataStore (at.schauer.gregor.vienna.datastore.remote)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">RMIDataStore</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    90%
  </span>
  <span class="absValue">
    (9/ 10)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    75.6%
  </span>
  <span class="absValue">
    (34/ 45)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.remote;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContextAware;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.datastore.DataStore;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>22</i>&nbsp;import org.apache.commons.lang3.ClassUtils;
<i>23</i>&nbsp;
<i>24</i>&nbsp;import javax.annotation.Nullable;
<i>25</i>&nbsp;import java.rmi.NotBoundException;
<i>26</i>&nbsp;import java.rmi.Remote;
<i>27</i>&nbsp;import java.rmi.RemoteException;
<i>28</i>&nbsp;import java.rmi.registry.LocateRegistry;
<i>29</i>&nbsp;import java.rmi.registry.Registry;
<i>30</i>&nbsp;import java.rmi.server.UnicastRemoteObject;
<i>31</i>&nbsp;import java.util.Collection;
<i>32</i>&nbsp;import java.util.HashSet;
<i>33</i>&nbsp;import java.util.Set;
<i>34</i>&nbsp;
<i>35</i>&nbsp;import static at.schauer.gregor.vienna.serializer.Serializer.CLASS_OBJECT_SEPARATOR;
<i>36</i>&nbsp;
<i>37</i>&nbsp;/**
<i>38</i>&nbsp; * @author Gregor Schauer
<i>39</i>&nbsp; */
<b class="nc"><i>40</i>&nbsp;public class RMIDataStore&lt;T extends Remote&gt; implements DataStore&lt;T&gt;, ViennaContextAware {</b>
<i>41</i>&nbsp;	protected ViennaContext ctx;
<i>42</i>&nbsp;	protected Registry registry;
<i>43</i>&nbsp;
<i>44</i>&nbsp;	public RMIDataStore() throws RemoteException {
<b class="fc"><i>45</i>&nbsp;		this(null, Registry.REGISTRY_PORT);</b>
<b class="fc"><i>46</i>&nbsp;	}</b>
<i>47</i>&nbsp;
<b class="fc"><i>48</i>&nbsp;	public RMIDataStore(@Nullable String host, int port) throws RemoteException {</b>
<i>49</i>&nbsp;		try {
<b class="fc"><i>50</i>&nbsp;			registry = LocateRegistry.getRegistry(host, port);</b>
<b class="nc"><i>51</i>&nbsp;		} catch (RemoteException e) {</b>
<b class="nc"><i>52</i>&nbsp;			registry = LocateRegistry.createRegistry(port);</b>
<b class="fc"><i>53</i>&nbsp;		}</b>
<b class="fc"><i>54</i>&nbsp;	}</b>
<i>55</i>&nbsp;
<i>56</i>&nbsp;	@Override
<i>57</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>58</i>&nbsp;	public &lt;E extends T&gt; E load(Class&lt;E&gt; type, Object identifier) {
<i>59</i>&nbsp;		try {
<b class="fc"><i>60</i>&nbsp;			return (E) registry.lookup(getName(type, identifier));</b>
<b class="fc"><i>61</i>&nbsp;		} catch (Exception e) {</b>
<b class="fc"><i>62</i>&nbsp;			throw new ViennaException(e);</b>
<i>63</i>&nbsp;		}
<i>64</i>&nbsp;	}
<i>65</i>&nbsp;
<i>66</i>&nbsp;	@Override
<i>67</i>&nbsp;	public Object save(T obj) {
<i>68</i>&nbsp;		try {
<b class="fc"><i>69</i>&nbsp;			String name = getName(obj);</b>
<b class="fc"><i>70</i>&nbsp;			registry.rebind(name, UnicastRemoteObject.exportObject(obj, 0));</b>
<b class="fc"><i>71</i>&nbsp;			return name;</b>
<b class="nc"><i>72</i>&nbsp;		} catch (RemoteException e) {</b>
<b class="nc"><i>73</i>&nbsp;			throw new ViennaException(e);</b>
<i>74</i>&nbsp;		}
<i>75</i>&nbsp;	}
<i>76</i>&nbsp;
<i>77</i>&nbsp;	@Override
<i>78</i>&nbsp;	public void delete(T obj) {
<i>79</i>&nbsp;		try {
<b class="fc"><i>80</i>&nbsp;			registry.unbind(getName(obj));</b>
<b class="nc"><i>81</i>&nbsp;		} catch (RemoteException e) {</b>
<b class="nc"><i>82</i>&nbsp;			throw new ViennaException(e);</b>
<b class="fc"><i>83</i>&nbsp;		} catch (NotBoundException e) {</b>
<b class="fc"><i>84</i>&nbsp;			throw new ViennaException(e);</b>
<b class="fc"><i>85</i>&nbsp;		}</b>
<b class="fc"><i>86</i>&nbsp;	}</b>
<i>87</i>&nbsp;
<i>88</i>&nbsp;	@Override
<i>89</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>90</i>&nbsp;	public &lt;E extends T&gt; Collection&lt;E&gt; list(Class&lt;E&gt; type) {
<i>91</i>&nbsp;		try {
<b class="fc"><i>92</i>&nbsp;			String foo = getName(type, null);</b>
<b class="fc"><i>93</i>&nbsp;			Set&lt;E&gt; localObjects = new HashSet&lt;E&gt;();</b>
<b class="fc"><i>94</i>&nbsp;			String[] names = registry.list();</b>
<b class="fc"><i>95</i>&nbsp;			for (String name : names) {</b>
<b class="fc"><i>96</i>&nbsp;				String className = name.substring(0, name.indexOf(CLASS_OBJECT_SEPARATOR));</b>
<b class="fc"><i>97</i>&nbsp;				Class&lt;E&gt; aClass = (Class&lt;E&gt;) ClassUtils.getClass(className);</b>
<b class="fc"><i>98</i>&nbsp;				String id = name.substring(name.indexOf(CLASS_OBJECT_SEPARATOR) + 1);</b>
<b class="fc"><i>99</i>&nbsp;				if (name.startsWith(foo) || type.isAssignableFrom(aClass)) {</b>
<b class="fc"><i>100</i>&nbsp;					Remote remote = load(aClass, id);</b>
<b class="fc"><i>101</i>&nbsp;					if (type.isAssignableFrom(remote.getClass())) {</b>
<b class="fc"><i>102</i>&nbsp;						localObjects.add((E) remote);</b>
<i>103</i>&nbsp;					}
<i>104</i>&nbsp;				}
<i>105</i>&nbsp;			}
<b class="fc"><i>106</i>&nbsp;			return localObjects;</b>
<b class="nc"><i>107</i>&nbsp;		} catch (RemoteException e) {</b>
<b class="nc"><i>108</i>&nbsp;			throw new ViennaException(e);</b>
<b class="nc"><i>109</i>&nbsp;		} catch (ClassNotFoundException e) {</b>
<b class="nc"><i>110</i>&nbsp;			throw new ViennaException(e);</b>
<i>111</i>&nbsp;		}
<i>112</i>&nbsp;	}
<i>113</i>&nbsp;
<i>114</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>115</i>&nbsp;	protected String getName(T obj) {
<b class="fc"><i>116</i>&nbsp;		Class&lt;T&gt; type = (Class&lt;T&gt;) obj.getClass();</b>
<b class="fc"><i>117</i>&nbsp;		return getName(type, ctx.getMetadataProvider(type).getIdentifier(obj));</b>
<i>118</i>&nbsp;	}
<i>119</i>&nbsp;
<i>120</i>&nbsp;	protected String getName(Class&lt;? extends T&gt; clazz, @Nullable Object id) {
<b class="fc"><i>121</i>&nbsp;		return clazz.getName() + CLASS_OBJECT_SEPARATOR + (id != null ? id : &quot;&quot;);</b>
<i>122</i>&nbsp;	}
<i>123</i>&nbsp;
<i>124</i>&nbsp;	@Override
<i>125</i>&nbsp;	public void setViennaContext(ViennaContext viennaContext) {
<b class="fc"><i>126</i>&nbsp;		this.ctx = viennaContext;</b>
<b class="fc"><i>127</i>&nbsp;	}</b>
<i>128</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
